package com.action
{
	import com.stage.item.Player;
	import com.utils.time.TimerClock;

	public class BaseAction
	{
		// 基本动作类型的定义
		public static const NULL:int = 0;
		public static const STAND:int = 1;
		public static const MOVE:int = 2;
		public static const ATTACK:int = 3;
		public static const HURT:int = 4;
		public static const JUMP:int = 5;
		public static const DIE:int = 6;
		public static const DEFENCE:int = 7;
		public static const TURN:int = 8;
		
		protected var _callback:Function; // 动作结束的回调
		protected var _dir:int;
		protected var _player:Player;
		protected var _height:Number = 0;
		protected var _type:int = NULL;
		protected var _actFinish:Boolean = false; // 动画播放结束标志
		protected var _downSpeed:Number = 0; // 垂直方向的移动速度
		protected var _speed:Number = 0; // 水平方向的移动速度，在子类里面处理实际的移动
		protected var _graAcc:Number = 1; // 重力加速度
		protected var _threadId:int = 0;
		protected var _data:Object;
		
		public function BaseAction(player:Player, data:Object = null)
		{
			_player = player;
			_data = data;
		}
		
		public function start(dir:int = 1, moveDir:int=0):void{
			_dir = dir;
			if(_threadId == 0){ // 第一次加监听
				_threadId = TimerClock.getInstance().addEnterFrame(enterFrame);	
			}	
			_player.addActionEndListener(actionEnd); // isStop会阻止同一帧被多次监听
		}
		
		/**
		 * 帧处理
		 */
		protected function enterFrame():void{ // 帧监听函数，资源动画之外的一些动画处理
			
		}
		
		/**
		 * 动作结束处理
		 */
		protected function actionEnd(player:Player):void{
			
		}
		
		/**
		 * 着陆时的处理
		 */
		protected function onLandGround():void{
			
		}
			
		/**
		 * 销毁这个动作
		 */
		public function end():void{
			_player.removeActionEndListener(actionEnd);
			if(_threadId > 0){
				TimerClock.getInstance().removeEnterFrame(_threadId);
				_threadId = 0;
			}
			_callback = null;
		}
		
		/**
		 * 动作结束的回调，
		 * 不同于actionEnd，actionEnd是角色资源的动作结束，这个结束还包括落地等等
		 * 持续性动作一般不设置回调，动作的转换是由指令触发的，动作被中断
		 * 非持续性动作需要设置回调，动作的转换可以由指令触发，这时是动作被中断，不触发回调，而动作结束按状态转换规则转换，需要通过回调来转换动作
		 */
		protected function actionCallback():void{
			if(_callback){
				_callback(this);
			}
		}
		
		/**
		 * 检查脚是否接触地面
		 * 这里地面没有水平高度的处理
		 */
		protected function checkGround():Boolean{
			if(_height + _downSpeed >= 0){
				_player.y -= _height;
				_height = 0;
				onLandGround();
				return true;
			}else{
				_player.y += _downSpeed;
				_height += _downSpeed;
				_downSpeed += _graAcc;
				return false;
			}
		}

		
		/**
		 * 判断是否脚着地
		 */
		protected function atGround():Boolean{
			if(_height >= 0){
				return true;
			}
			return false;
		}
		
		public function set callback(value:Function):void
		{
			_callback = value;
		}
		
		public function get player():Player{
			return _player;
		}
		
		public function get dir():int{
			return _dir;
		}
		
		public function get height():Number{
			return _height;	
		}
		
		public function set height(value:Number):void{
			_height = value;
		}
		
		public function get type():int{
			return _type;
		}
		
		public function get downSpeed():Number{
			return _downSpeed;
		}
		
		public function set downSpeed(value:Number):void{
			_downSpeed = value;
		}
		
		public function get data():Object{
			return _data;
		}

		public function get speed():Number
		{
			return _speed;
		}

		public function set speed(value:Number):void
		{
			_speed = value;
		}

	}
}